﻿using System;
using System.Collections.Generic;
using System.Text;
using Business.Entities;
using System.Data;
using System.Data.SqlClient;

namespace Data.Database
{
    public class CursoAdapter:Adapter
    {
        public List<Curso> GetAll()
        {
            
            List<Curso> cursos = new List<Curso>();
            try
            {
                this.OpenConnection();
                SqlCommand cmdCursos = new SqlCommand("select * from cursos", SqlConn);
                SqlDataReader drCursos = cmdCursos.ExecuteReader();


                while (drCursos.Read())
                {
                    Curso cur = new Curso();
                    cur.ID = (int)drCursos["id_curso"];
                    cur.Idmateria=(int)drCursos["id_materia"];
                    cur.Idcomision = (int)drCursos["id_comision"];
                    cur.Aniocalendario = (int)drCursos["anio_calendario"];
                    cur.Cupo=(int)drCursos["cupo"];
                    cursos.Add(cur);

                }
                drCursos.Close();
            }
            catch (Exception Ex)
            {
                Exception ExcepcionManejada =
                new Exception("Error al recuperar lista de cursos", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

            return cursos;
        }

        public Business.Entities.Curso GetOne(int ID)
        {
            Curso cur = new Curso();

           try
          {
              this.OpenConnection();
              SqlCommand cmdcursos = new SqlCommand("select  * from cursos where id_curso = @id", SqlConn);
              cmdcursos.Parameters.Add("@id", SqlDbType.Int).Value = ID;
              SqlDataReader drCursos = cmdcursos.ExecuteReader();
              if (drCursos.Read())
              {
                    
                  cur.ID = (int)drCursos["id_curso"];
                  cur.Idmateria = (int)drCursos["id_materia"];
                  cur.Idcomision = (int)drCursos["id_comision"];
                  cur.Aniocalendario = (int)drCursos["anio_calendario"];
                  cur.Cupo = (int)drCursos["cupo"];

              }
              drCursos.Close();
          }
          catch (Exception Ex)
          {
              Exception ExcepcionManejada =
              new Exception("Error al recuperar datos del curso", Ex);
              throw ExcepcionManejada;
          }
          finally
          {
              this.CloseConnection();
          }

            return cur;
        }


        public void Delete(int ID)
        {
            
            try
            {
                this.OpenConnection();
                SqlCommand cmdDelete = new SqlCommand("delete from cursos where id_curso=@id", SqlConn);
                cmdDelete.Parameters.Add("@id", SqlDbType.Int).Value = ID;
                cmdDelete.ExecuteNonQuery();
            }

            catch (Exception Ex)
            {
                Exception ExcepcionManejada = new Exception("Error al eliminar curso", Ex);
                throw ExcepcionManejada;

            }

            finally
            {
                this.CloseConnection();
            }


        }


        protected void Update(Curso cur)
        {
	    try
	        {
	        this.OpenConnection();
	        SqlCommand cmdSave=new SqlCommand(
            "UPDATE cursos set id_materia=@id_materia, id_comision=@id_comision, " +
            "anio_calendario=@anio_calendario, cupo=@cupo " +
	        "WHERE id_curso=@id", SqlConn);
	        cmdSave.Parameters.Add("@id", SqlDbType.Int).Value=cur.ID;
            cmdSave.Parameters.Add("@id_materia", SqlDbType.Int).Value = cur.Idmateria;
            cmdSave.Parameters.Add("@id_comision", SqlDbType.Int).Value = cur.Idcomision;
            cmdSave.Parameters.Add("@anio_calendario", SqlDbType.Int).Value = cur.Aniocalendario;
            cmdSave.Parameters.Add("@cupo", SqlDbType.Int).Value = cur.Cupo;
	        cmdSave.ExecuteNonQuery();
	        }

	 catch(Exception Ex)
	    {
	    Exception ExcepcionManejada= new Exception("Error al modificar datos del curso",Ex);
	    throw ExcepcionManejada;
	    }
	 finally
	    {
	    this.CloseConnection();
	    }
	    
   }


     protected void Insert(Curso cur)
        {   
	        try
	        {
	        this.OpenConnection();
	        SqlCommand cmdSave=new SqlCommand(
            "insert into cursos(id_materia,id_comision,anio_calendario,cupo) " +
            "values(@id_materia,@id_comision,@anio_calendario,@cupo) " +
	        "select @@identity", SqlConn);
            
            cmdSave.Parameters.Add("@id_materia", SqlDbType.Int).Value = cur.Idmateria;
            cmdSave.Parameters.Add("@id_comision", SqlDbType.Int).Value = cur.Idcomision;
            cmdSave.Parameters.Add("@anio_calendario", SqlDbType.Int).Value = cur.Aniocalendario;
            cmdSave.Parameters.Add("@cupo", SqlDbType.Int).Value = cur.Cupo;
        	    
	        cur.ID=Decimal.ToInt32((decimal)cmdSave.ExecuteScalar());
	        }

	        catch(Exception Ex)
	        {
	        Exception ExcepcionManejada= new Exception("Error al crear curso",Ex);
	        throw ExcepcionManejada;
	        }
	        finally
	        {
	        this.CloseConnection();
	        }
	        
        }





        public void Save(Curso cur)
        {
            if (cur.State == BussinessEntity.States.Deleted)
            {
                this.Delete(cur.ID);
            }
            else if (cur.State == BussinessEntity.States.New)
            {
                this.Insert(cur);
            }
            else if (cur.State == BussinessEntity.States.Modified)
            {
                this.Update(cur);
            }
            cur.State = BussinessEntity.States.Unmodified; 
            
        }
    }

}

